#pragma once

#include <iostream>
#include <string>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <mutex>
#include <unordered_map>
#include "cppjieba/Jieba.hpp"
#include "log.hpp"

namespace ns_util
{
    class FileUtil
    {
    public:
        static bool ReadFile(const std::string& file_path, std::string* ptext)
        {
            std::ifstream input(file_path, std::ios::in);
            if(!input.is_open())
            {
                std::cerr << "read " << file_path << " failed" << std::endl;
                return false;
            }

            // 逐行读取文件内容到line中
            std::string line;
            while(std::getline(input, line))
            {
                *ptext += line;
            }
            // 别忘记关闭文件
            input.close();
            return true;
        }
    };

    class StringUtil
    {
    public:
        static void SpiltString(const std::string& str, std::vector<std::string>* pvs, const std::string& sep)
        {
            boost::split(*pvs, str, boost::is_any_of(sep), boost::token_compress_on);
        }
    };

    const char* const DICT_PATH = "./dict/jieba.dict.utf8";                                                                                                                                                        
    const char* const HMM_PATH = "./dict/hmm_model.utf8";    
    const char* const USER_DICT_PATH = "./dict/user.dict.utf8";    
    const char* const IDF_PATH = "./dict/idf.utf8";    
    const char* const STOP_WORD_PATH = "./dict/stop_words.utf8";    

    // 改造为单例模式
    class JiebaUtil
    {
    private:
        JiebaUtil():jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH)
        {}
        JiebaUtil(const JiebaUtil&) = delete;
        JiebaUtil& operator= (const JiebaUtil&) = delete;

        // 将停止词读取到停止词集合中
        void InitJieba()
        {
            std::ifstream ifs(STOP_WORD_PATH, std::ios::in);
            if(!ifs.is_open())
            {
                LOG(FATAL, "打开文件失败：" + std::string(STOP_WORD_PATH));
            }

            std::string line;
            while(std::getline(ifs, line))
            {
                stops_map.insert({line, true});
            }
        }

        // 分词中去掉限制词
        void CutstringPlus(const std::string& str, std::vector<std::string>* pvs)
        {
            jieba.CutForSearch(str, *pvs);
            
            // 遍历分词数组，查找是否有分词
            for(auto iter = pvs->begin(); iter != pvs->end(); )
            {
                auto pos = stops_map.find(*iter);
                // 如果找到了,删除此词
                if(pos != stops_map.end())
                {
                    iter = pvs->erase(iter);
                }
                else
                {
                    ++iter;
                }
            }
        }

    public:
        static JiebaUtil* GetInstance()
        {
            static std::mutex mtx;
            if(pins == nullptr)
            {
                mtx.lock();
                if(pins == nullptr)
                {
                    pins = new JiebaUtil();
                    pins->InitJieba();
                }
            }
            return pins;
        }
        void CutString(const std::string& str, std::vector<std::string>* pvs)
        {
            // jieba.CutForSearch(str, *pvs);
            CutstringPlus(str, pvs);
        }

    private:
        cppjieba::Jieba jieba;
        static JiebaUtil* pins;
        std::unordered_map<std::string, bool> stops_map;; // 停止词合集,利用map去重并且方便查找,如果用vector则不方便查找
    };
    JiebaUtil* JiebaUtil::pins = nullptr;
}